home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / EasyPHP-2.0b1-setup.exe / {app} / phpmyadmin / db_datadict.php < prev    next >
Encoding:
PHP Script  |  2006-11-18  |  10.4 KB  |  322 lines

  1. <?php
  2. /* $Id: db_datadict.php 9179 2006-07-09 23:24:45Z lem9 $ */
  3.  
  4.  
  5. /**
  6.  * Gets the variables sent or posted to this script, then displays headers
  7.  */
  8. require_once('./libraries/common.lib.php');
  9.  
  10. if (!isset($selected_tbl)) {
  11.     require_once('./libraries/header.inc.php');
  12. }
  13.  
  14.  
  15. /**
  16.  * Gets the relations settings
  17.  */
  18. require_once('./libraries/relation.lib.php');
  19. require_once('./libraries/transformations.lib.php');
  20.  
  21. $cfgRelation  = PMA_getRelationsParam();
  22.  
  23. /**
  24.  * Check parameters
  25.  */
  26. PMA_checkParameters(array('db'));
  27.  
  28. /**
  29.  * Defines the url to return to in case of error in a sql statement
  30.  */
  31. if (isset($table)) {
  32.     $err_url = 'tbl_properties.php?' . PMA_generate_common_url($db, $table);
  33. } else {
  34.     $err_url = 'db_details.php?' . PMA_generate_common_url($db);
  35. }
  36.  
  37. if ($cfgRelation['commwork']) {
  38.     $comment = PMA_getComments($db);
  39.  
  40.     /**
  41.      * Displays DB comment
  42.      */
  43.     if (is_array($comment)) {
  44.         ?>
  45.     <p> <?php echo $strDBComment; ?> 
  46.         <i><?php echo htmlspecialchars( implode( ' ', $comment ) ); ?></i></p>
  47.         <?php
  48.     } // end if
  49. }
  50.  
  51. /**
  52.  * Selects the database and gets tables names
  53.  */
  54. PMA_DBI_select_db($db);
  55. $rowset = PMA_DBI_query('SHOW TABLES FROM ' . PMA_backquote($db) . ';', null, PMA_DBI_QUERY_STORE);
  56.  
  57. $count  = 0;
  58. while ($row = PMA_DBI_fetch_assoc($rowset)) {
  59.     $myfieldname = 'Tables_in_' . htmlspecialchars($db);
  60.     $table        = $row[$myfieldname];
  61.     if ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100) {
  62.         $comments = PMA_getComments($db, $table);
  63.     }
  64.  
  65.     if ($count != 0) {
  66.         echo '<div style="page-break-before: always;">' . "\n";
  67.     } else {
  68.         echo '<div>' . "\n";
  69.     }
  70.  
  71.     echo '<h2>' . $table . '</h2>' . "\n";
  72.  
  73.     /**
  74.      * Gets table informations
  75.      */
  76.     // The 'show table' statement works correct since 3.23.03
  77.     $showtable    = PMA_DBI_get_tables_full( $db, $table );
  78.     $num_rows     = (isset($showtable[$table]['TABLE_ROWS']) ? $showtable[$table]['TABLE_ROWS'] : 0);
  79.     $show_comment = (isset($showtable[$table]['TABLE_COMMENT']) ? $showtable[$table]['TABLE_COMMENT'] : '');
  80.     unset( $showtable );
  81.  
  82.  
  83.     /**
  84.      * Gets table keys and retains them
  85.      */
  86.  
  87.     PMA_DBI_select_db($db);
  88.     $result       = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($table) . ';');
  89.     $primary      = '';
  90.     $indexes      = array();
  91.     $lastIndex    = '';
  92.     $indexes_info = array();
  93.     $indexes_data = array();
  94.     $pk_array     = array(); // will be use to emphasis prim. keys in the table
  95.                              // view
  96.     while ($row = PMA_DBI_fetch_assoc($result)) {
  97.         // Backups the list of primary keys
  98.         if ($row['Key_name'] == 'PRIMARY') {
  99.             $primary   .= $row['Column_name'] . ', ';
  100.             $pk_array[$row['Column_name']] = 1;
  101.         }
  102.         // Retains keys informations
  103.         if ($row['Key_name'] != $lastIndex ){
  104.             $indexes[] = $row['Key_name'];
  105.             $lastIndex = $row['Key_name'];
  106.         }
  107.         $indexes_info[$row['Key_name']]['Sequences'][]     = $row['Seq_in_index'];
  108.         $indexes_info[$row['Key_name']]['Non_unique']      = $row['Non_unique'];
  109.         if (isset($row['Cardinality'])) {
  110.             $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
  111.         }
  112.         // I don't know what does following column mean....
  113.         // $indexes_info[$row['Key_name']]['Packed']          = $row['Packed'];
  114.  
  115.         $indexes_info[$row['Key_name']]['Comment']     = $row['Comment'];
  116.  
  117.         $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name']  = $row['Column_name'];
  118.         if (isset($row['Sub_part'])) {
  119.             $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
  120.         }
  121.  
  122.     } // end while
  123.     if ($result) {
  124.         PMA_DBI_free_result($result);
  125.     }
  126.  
  127.  
  128.     /**
  129.      * Gets fields properties
  130.      */
  131.     $result      = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
  132.     $fields_cnt  = PMA_DBI_num_rows($result);
  133.  
  134.  
  135. // We need this to correctly learn if a TIMESTAMP is NOT NULL, since
  136. // SHOW FULL FIELDS or INFORMATION_SCHEMA incorrectly says NULL
  137. // and SHOW CREATE TABLE says NOT NULL (tested
  138. // in MySQL 4.0.25 and 5.0.21, http://bugs.mysql.com/20910).
  139.  
  140.     $show_create_table = PMA_DBI_fetch_value(
  141.         'SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table),
  142.         0, 1);
  143.     $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
  144.  
  145.     // Check if we can use Relations (Mike Beck)
  146.     if (!empty($cfgRelation['relation'])) {
  147.         // Find which tables are related with the current one and write it in
  148.         // an array
  149.         $res_rel = PMA_getForeigners($db, $table);
  150.  
  151.         if (count($res_rel) > 0) {
  152.             $have_rel = TRUE;
  153.         } else {
  154.             $have_rel = FALSE;
  155.         }
  156.     } else {
  157.         $have_rel = FALSE;
  158.     } // end if
  159.  
  160.  
  161.     /**
  162.      * Displays the comments of the table if MySQL >= 3.23
  163.      */
  164.     if (!empty($show_comment)) {
  165.         echo $strTableComments . ': ' . $show_comment . '<br /><br />';
  166.     }
  167.  
  168.     /**
  169.      * Displays the table structure
  170.      */
  171.     ?> 
  172.  
  173. <table width="100%" class="print">
  174. <tr><th width="50"><?php echo $strField; ?></th>
  175.     <th width="80"><?php echo $strType; ?></th>
  176. <?php /*    <th width="50"><?php echo $strAttr; ?></th>*/ ?>
  177.     <th width="40"><?php echo $strNull; ?></th>
  178.     <th width="70"><?php echo $strDefault; ?></th>
  179. <?php /*    <th width="50"><?php echo $strExtra; ?></th>*/ ?>
  180.     <?php
  181.     if ($have_rel) {
  182.         echo '    <th>' . $strLinksTo . '</th>' . "\n";
  183.     }
  184.     if ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100) {
  185.         echo '    <th>' . $strComments . '</th>' . "\n";
  186.     }
  187.     if ($cfgRelation['mimework']) {
  188.         echo '    <th>MIME</th>' . "\n";
  189.     }
  190.     ?>
  191. </tr>
  192.     <?php
  193.     $odd_row = true;
  194.     while ($row = PMA_DBI_fetch_assoc($result)) {
  195.  
  196.         $type             = $row['Type'];
  197.         // reformat mysql query output - staybyte - 9. June 2001
  198.         // loic1: set or enum types: slashes single quotes inside options
  199.         if (preg_match('@^(set|enum)\((.+)\)$@i', $type, $tmp)) {
  200.             $tmp[2]       = substr(preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1);
  201.             $type         = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
  202.             $type_nowrap  = '';
  203.  
  204.             $binary       = 0;
  205.             $unsigned     = 0;
  206.             $zerofill     = 0;
  207.         } else {
  208.             $binary       = stristr($row['Type'], 'binary');
  209.             $unsigned     = stristr($row['Type'], 'unsigned');
  210.             $zerofill     = stristr($row['Type'], 'zerofill');
  211.             $type_nowrap  = ' nowrap="nowrap"';
  212.             $type         = preg_replace('@BINARY@i', '', $type);
  213.             $type         = preg_replace('@ZEROFILL@i', '', $type);
  214.             $type         = preg_replace('@UNSIGNED@i', '', $type);
  215.             if (empty($type)) {
  216.                 $type     = ' ';
  217.             }
  218.         }
  219.         $strAttribute     = ' ';
  220.         if ($binary) {
  221.             $strAttribute = 'BINARY';
  222.         }
  223.         if ($unsigned) {
  224.             $strAttribute = 'UNSIGNED';
  225.         }
  226.         if ($zerofill) {
  227.             $strAttribute = 'UNSIGNED ZEROFILL';
  228.         }
  229.         if (!isset($row['Default'])) {
  230.             if ($row['Null'] != '' && $row['Null'] != 'NO') {
  231.                 $row['Default'] = '<i>NULL</i>';
  232.             }
  233.         } else {
  234.             $row['Default'] = htmlspecialchars($row['Default']);
  235.         }
  236.         $field_name = htmlspecialchars($row['Field']);
  237.  
  238.         // here, we have a TIMESTAMP that SHOW FULL FIELDS reports as having the
  239.         // NULL attribute, but SHOW CREATE TABLE says the contrary. Believe
  240.         // the latter.
  241.     // TODO: merge this logic with the one in tbl_properties_structure.php
  242.     // or move it in a function similar to PMA_DBI_get_columns_full()
  243.     // but based on SHOW CREATE TABLE because information_schema
  244.     // cannot be trusted in this case (MySQL bug)
  245.         if (!empty($analyzed_sql[0]['create_table_fields'][$field_name]['type']) && $analyzed_sql[0]['create_table_fields'][$field_name]['type'] == 'TIMESTAMP' && $analyzed_sql[0]['create_table_fields'][$field_name]['timestamp_not_null']) {
  246.             $row['Null'] = '';
  247.         }
  248.         ?> 
  249. <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
  250.     <td nowrap="nowrap">
  251.         <?php
  252.         if (isset($pk_array[$row['Field']])) {
  253.             echo '<u>' . $field_name . '</u>';
  254.         } else {
  255.             echo $field_name;
  256.         }
  257.         ?> 
  258.     </td>
  259.     <td<?php echo $type_nowrap; ?> xml:lang="en" dir="ltr"><?php echo $type; ?></td>
  260. <?php /*    <td<?php echo $type_nowrap; ?>><?php echo $strAttribute; ?></td>*/ ?>
  261.     <td><?php echo (($row['Null'] == '' || $row['Null'] == 'NO') ? $strNo : $strYes); ?></td>
  262.     <td nowrap="nowrap"><?php if (isset($row['Default'])) { echo $row['Default']; } ?></td>
  263. <?php /*    <td<?php echo $type_nowrap; ?>><?php echo $row['Extra']; ?></td>*/ ?>
  264.         <?php
  265.         if ($have_rel) {
  266.             echo '    <td>';
  267.             if (isset($res_rel[$field_name])) {
  268.                 echo htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field']);
  269.             }
  270.             echo '</td>' . "\n";
  271.         }
  272.         if ($cfgRelation['commwork'] || PMA_MYSQL_INT_VERSION >= 40100) {
  273.             echo '    <td>';
  274.             if (isset($comments[$field_name])) {
  275.                 echo htmlspecialchars($comments[$field_name]);
  276.             }
  277.             echo '</td>' . "\n";
  278.         }
  279.         if ($cfgRelation['mimework']) {
  280.             $mime_map = PMA_getMIME($db, $table, true);
  281.  
  282.             echo '    <td>';
  283.             if (isset($mime_map[$field_name])) {
  284.                 echo htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype']));
  285.             }
  286.             echo '</td>' . "\n";
  287.         }
  288.         ?>
  289. </tr>
  290.         <?php
  291.     } // end while
  292.     PMA_DBI_free_result( $result );
  293.     $count++;
  294.     ?> 
  295. </table>
  296. </div>
  297.     <?php
  298. } //ends main while
  299.  
  300. /**
  301.  * Displays the footer
  302.  */
  303. ?> 
  304. <script type="text/javascript" language="javascript">
  305. //<![CDATA[
  306. function printPage()
  307. {
  308.     document.getElementById('print').style.visibility = 'hidden';
  309.     // Do print the page
  310.     if (typeof(window.print) != 'undefined') {
  311.         window.print();
  312.     }
  313.     document.getElementById('print').style.visibility = '';
  314. }
  315. //]]>
  316. </script>
  317. <?php
  318. echo '<br /><br /><input type="button" id="print" value="' . $strPrint . '" onclick="printPage()" />';
  319.  
  320. require_once('./libraries/footer.inc.php');
  321. ?>
  322.